new Vue({
    el:'#main',
    data:()=>({
        page:1,
        pageSize:10,
        port_id:0,
        year:0,//年
        month:0,//月
        montharr:[],
        port:[],
        listdata:{
            content:[],
            footer:{},
        },
        batch:[],
        allche:[],
        auth_ids:$('#adminauth_ids').text()
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            this.allche= []
            $('.alert').hide()
            $.ajax({
                url:'/admin/receipt/list',
                type:'post',
                data:{
                    'year':this.year,
                    'month':this.month,
                    'port_id':this.port_id,
                    'page':this.page,
                    'pageSize':this.pageSize
                },
                success:(data)=>{
                    data = JSON.parse(data)                    
                    this.port = data.port
                    this.listdata = data.buy_po_pack
                    this.listdata.content.forEach((item)=>{
                        item.buy_po_pack.forEach(element => {
                            this.allche.push(element.id)
                        });
                    })
                    if(this.listdata.packNum)
                    this.Resetnav(this.page,this.listdata.packNum)
                }
            })
        },
        changeterminal(id){//修改码头
            this.port_id=id
            this.getdata()
        },
        changemonth(date){
            this.year=date.year
            this.month=date.month
            this.getdata()
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        allsel(event){
            if($(event.target).prop('checked')){
                $(event.target).parents('table').find('input').prop('checked',true)
                this.batch=this.allche
            }else{
                $(event.target).parents('table').find('input').prop('checked',false)
                this.batch=[]
            }
        },
        onesel(did){
            if(this.batch.indexOf(did)==-1){
                this.batch.push(did)
            }else{
                this.batch.splice(this.batch.indexOf(did),1)
            }
            var isall=true
            $('td input').each(function() {
                if(!$(this).prop('checked')){
                    isall=false
                }
            })
            $('th input').prop('checked',isall)
        },
        setselectportId(){
            $('#selectportId').val(0)
        },
        movePort(){
            if($('#selectportId').val() == 0){
                this.point({status:2,msg:'请选择需要移入的码头'},'#moveto_Modal') 
                return false
            } 
            
            var data = {
                port_id:$('#selectportId').val(),
                buy_po_pack_ids:this.batch
            }

            $.ajax({
                url:'/admin/receipt/moveProt',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)      
                    this.point(data,'#moveto_Modal') 
                    if(data.status == 1){
                        this.batch = []
                        $('#allsel').attr('checked',false)
                        $('.listdataItem').attr('checked',false)
                        this.getdata()
                        this.point(data,'#mainAlert',2)

                    }
                }
            })
        },
        point(data,modelId,type=1){
            change_mes_type(modelId+' .alert',data.status)
            if(data.status == 1){
                if(type == 1){
                    if(modelId != '.container-fluid') $(modelId).modal('toggle')
                }
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }else{
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }
        },
    },
    mounted(){
        var year=new Date().getFullYear()
        var current=new Date().getMonth()+1
        this.year=year
        this.month=current
        this.montharr.push({year:year,month:current,splyear:year.toString().slice(2)-0})
        for(var i=1;i<6;i++){
            current++
            if(current>12){
                year++
                current=1
            }
            this.montharr.push({year:year,month:current,splyear:year.toString().slice(2)-0})
        }
        this.getdata()
        $('.loding').hide()
    }
})
